import router from '@/javascript/router/index'
const JSON = { 'Content-Type': 'application/json' }
const FORM = { 'Content-Type': 'application/x-www-form-urlencoded' }
const FILE = { 'Content-Type': 'multipart/form-data' }
axios.defaults.baseURL = process.env.NODE_ENV === 'development' ? '/api' : '/'
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8'
axios.defaults.timeout = 100000
axios.defaults.withCredentials = true
axios.interceptors.request.use(
    config => {
        if (useStore().token) {
            config.headers.access_token = useStore().token
        }
        return config
    },
    error => {
        return Promise.reject(error)
    }
)
axios.interceptors.response.use(
    response => {
        return Promise.resolve(response)
    },
    error => {
        if (error.code === 'ERR_NETWORK') {
            setTimeout(() => {
                showToast('服务器正在重启，请稍后再试...')
            }, 200)
            return
        }

        //校验失败提示
        if (error.response.data.code === 'method_argument_not_valid') {
            if (error.response.data.errors && error.response.data.errors.length) {
                let str = []
                error.response.data.errors.forEach(element => {
                    //showToast(`"${element.name}"${element.message}`)
                })
            }
            return Promise.reject(error.response.data)
        }

        if (error.response.data.code === 'insufficient_authentication') {
            if (window.appType === '钉钉') {
                Modal.confirm({
                    title: '提示',
                    content: `当前状态已更新，请刷新或重新进入~`,
                    okText: '立即刷新',
                    maskClosable: false,
                    onOk: () => {
                        window.location.reload()
                    },
                })
            } else {
                showToast('当前状态已失效，请手动登录')
                setTimeout(() => {
                    router.push('/ocean')
                }, 0)
            }
            return
        }

        //showToast(error.response.data.message)
        return Promise.reject(error.response.data)
    }
)

axios.postJson = (url, data) => {
    return new Promise((resolve, reject) => {
        axios({
            headers: JSON,
            method: 'post',
            url,
            data: data,
        })
            .then(res => {
                resolve(res.data)
            })
            .catch(err => {
                reject(err)
            })
    })
}
axios.postForm = (url, data) => {
    return new Promise((resolve, reject) => {
        axios({
            headers: FORM,
            method: 'post',
            url,
            data: qs.stringify(data),
        })
            .then(res => {
                resolve(res.data)
            })
            .catch(err => {
                reject(err)
            })
    })
}

axios.postFile = (url, data) => {
    return new Promise((resolve, reject) => {
        axios({
            headers: FILE,
            method: 'post',
            responseType: 'blob',
            url,
            data: data,
        })
            .then(res => {
                resolve(res)
            })
            .catch(err => {
                reject(err)
            })
    })
}
axios.get = (url, data) => {
    return new Promise((resolve, reject) => {
        axios({
            method: 'get',
            url,
            params: data,
        })
            .then(res => {
                resolve(res.data)
            })
            .catch(err => {
                reject(err)
            })
    })
}

axios.http = request => {
    return new Promise((resolve, reject) => {
        axios(request)
            .then(res => {
                resolve(res)
            })
            .catch(err => {
                reject(err)
            })
    })
}

export default axios
